<template>
    <div>
        <div>
            昵称:
            <Input type="text" v-model="searchData.username" style="width: 150px;" clearable />
            手机号:
            <Input type="text" v-model="searchData.mobile" style="width: 150px;" clearable />
            驾校：
            <Select v-model="searchData.school_id" filterable style="width: 150px;">
                <Option :value="val.id" v-for="val in schoolList" :key="val.id">{{ val.name }}</Option>
            </Select>
            团队长：
            <Select v-model="searchData.is_team_leader" style="width: 150px;">
                <Option value="1">是</Option>
                <Option value="0">否</Option>
            </Select>
            时间：
            <Date-picker type="daterange" placeholder="开始日期" v-model="searchData.create_time"></Date-picker>
            <Button type="primary" @click="search()" style="margin-left: 10px;">搜索</Button>
            <Button @click="reset()" style="margin-left: 10px;">重置</Button>
            <Button type="info" @click="edit(false, 0)" style="margin-left: 10px;">添加</Button>
        </div>
        <div style="margin-top: 10px;">
            <Table :columns="columns" :data="list" max-height="800" border stripe size="small">
                <template slot-scope="{ row , index}" slot="wxSlot">
                    <img :src="row.wx_img">
                </template>
                <template slot-scope="{ row , index}" slot="makeSlot">
                    <Button type="success" @click="edit(row, 1)" size="small">详情</Button>
                    <Button type="info" @click="edit(row, 0)" size="small" style="margin-left: 1px;">编辑</Button>
                    <Button type="error" @click="del(row.id)" style="margin-left: 1px;" size="small">删除</Button>
                </template>
            </Table>
        </div>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize" :currentPage="searchData.page">
        </Paginate>

        <!-- 编辑 -->
        <Modal :title="formItem.id > 0 ? '修改教练' : '添加教练'" v-model="editModal" :width="40"
            :footer-hide="!!formItem.is_detail" :mask-closable="false">
            <Form :model="formItem" :label-width="90" :disabled="!!formItem.is_detail">
                <FormItem label="姓名">
                    <Input v-model="formItem.username" maxlength="15"></Input>
                </FormItem>
                <FormItem label="手机号">
                    <Input v-model="formItem.mobile" maxlength="11"></Input>
                </FormItem>
                <!-- <FormItem label="密码" v-if="!formItem.is_detail">
                    <Input v-model="formItem.password"></Input>
                </FormItem> -->
                <FormItem label="驾校">
                    <Select v-model="formItem.school_id" filterable>
                        <Option :value="val.id" v-for="val in schoolList" :key="val.id">{{ val.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="团队长">
                    <RadioGroup v-model="formItem.is_team_leader">
                        <Radio label="0" border>否</Radio>
                        <Radio label="1" border>是</Radio>
                    </RadioGroup>
                </FormItem>
                <!-- <FormItem label="所属团队" v-if="formItem.is_team_leader == 0">
                    <Select v-model="formItem.pid" filterable>
                        <Option value="0">无</Option>
                        <Option :value="val.id" v-for="val in coachList" :key="val.id">{{ val.team }}</Option>
                    </Select>
                </FormItem> -->
                <FormItem label="团队名称" v-if="formItem.is_team_leader == 1">
                    <Input v-model="formItem.team"></Input>
                </FormItem>
                <FormItem label="教车类型">
                    <Input v-model="formItem.edu_type"></Input>
                </FormItem>
                <FormItem label="服务分">
                    <Input v-model="formItem.service_score" type="number"></Input>
                </FormItem>
                <FormItem label="驾照">
                    <Row>
                        <MyUpload :maxNumber="1" @getImages="getImg3" :defaultImags="[formItem.drive_img]"></MyUpload>
                        身份证正面:
                        <MyUpload :maxNumber="1" @getImages="getImg4" :defaultImags="[formItem.front_img]"></MyUpload>
                        身份证反面:
                        <MyUpload :maxNumber="1" @getImages="getImg5" :defaultImags="[formItem.back_img]"></MyUpload>
                    </Row>
                </FormItem>
                <FormItem label="状态" v-if="!formItem.is_detail">
                    <RadioGroup v-model="formItem.status">
                        <Radio label="0" border>冻结</Radio>
                        <Radio label="1" border>正常</Radio>
                    </RadioGroup>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long @click="editModalOk">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import Paginate from "_c/paginate";
import ShowImg from '_c/show_img'
import MyUpload from "_c/my-upload"
export default {
    name: "userLevel",
    components: {
        Paginate, ShowImg, MyUpload
    },
    data() {
        return {
            searchData: {
                sort: "desc",
                order_by: "id",
                page: 1,
                limit: 10
            },
            // 列表
            columns: [

                {
                    title: '姓名',
                    align: 'center',
                    key: 'username',
                },
                {
                    title: '手机号',
                    align: 'center',
                    key: 'mobile',
                },

                {
                    title: '驾校',
                    align: 'center',
                    key: 'school_name',
                },
                {
                    title: '团队',
                    align: 'center',
                    key: 'team',
                },
                {
                    title: '团队长',
                    align: 'center',
                    key: 'is_team_leader',
                    render: (h, param) => {
                        let txt = '';
                        let color = '';
                        if (param.row.is_team_leader == 0) {
                            txt = '不是';
                            color = 'red';
                        } else {
                            txt = '是';
                            color = 'blue'
                        }
                        return h('div', [
                            h('span', {
                                style: {
                                    color: color
                                }
                            }, txt),
                        ]);
                    },
                },
                {
                    title: '类型',
                    align: 'center',
                    key: 'edu_type',
                },
                {
                    title: '状态',
                    align: 'center',
                    key: 'status',
                    render: (h, param) => {
                        let txt = '';
                        let color = '';
                        if (param.row.status == 0) {
                            txt = '冻结';
                            color = 'red';
                        } else {
                            txt = '正常';
                            color = 'blue'
                        }
                        return h('div', [
                            h('span', {
                                style: {
                                    color: color
                                }
                            }, txt),
                        ]);
                    },
                },
                {
                    title: '服务分',
                    align: 'center',
                    key: 'service_score',
                },
                {
                    title: '接单数量',
                    align: 'center',
                    key: 'accept_order_num',
                },
                {
                    title: '总收入',
                    align: 'center',
                    key: 'order_amount',
                },
                {
                    title: '添加时间',
                    align: 'center',
                    key: 'create_time',
                },
                {
                    title: '操作',
                    align: 'center',
                    slot: 'makeSlot',
                    width: "180",
                },
            ],
            // 数据
            list: [],
            total: 0,
            // 编辑框
            editModal: false,
            formItem: {},
            coachList: [],
            schoolList: [],
        }
    },
    methods: {
        getImg3(items) {
            items.forEach(item => {
                this.formItem.drive_img = item.url;
            });
        },
        getImg4(items) {
            items.forEach(item => {
                this.formItem.front_img = item.url;
            });
        },
        getImg5(items) {
            items.forEach(item => {
                this.formItem.back_img = item.url;
            });
        },
        getList() {
            this.$axios('/admin/coach/getList', this.searchData).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                sort: "desc",
                order_by: "id",
                page: 1,
                limit: 10
            }
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        edit(row, is_detail) {
            if (row) {
                this.formItem = { ...row };
                this.formItem.status = this.formItem.status + '';
                this.formItem.is_team_leader = this.formItem.is_team_leader + '';
                this.formItem.is_detail = is_detail;
            } else {
                this.formItem = {
                    id: 0,
                    status: "1",
                    is_detail: is_detail,
                    is_team_leader: "0",
                    service_score: ""
                };
            }
            // this.getCoachList();
            this.editModal = true;
        },
        editModalOk() {
            this.$axios('/admin/coach/save', this.formItem).then(res => {
                this.$Message.success('执行成功');
                this.editModal = false;
                this.getList();
            });
        },
        del(id) {
            this.$Modal.confirm({
                title: '删除操作',
                content: "<p>您确定要删除用户吗？</p>",
                onOk: () => {
                    this.$axios('/admin/coach/del', { id: id }).then(res => {
                        this.$Message.success('执行成功');
                        this.getList();
                    });
                },
                onCancel: () => {
                    this.$Message.info('取消操作');
                }
            });
        },
        // 获取项目
        getCoachList(id) {
            this.$axios("/admin/coach/getTeamList", { id: id }).then(res => {
                this.coachList = res;
            });
        },
        getSchoolList() {
            this.$axios("/admin/school/school/getArr").then(res => {
                this.schoolList = res;
            });
        },

    },
    created() {
        this.getList();
        this.getSchoolList();
    }
}
</script>
<style></style>
